import express from 'express';
import UserDao from '../dao/UserDao.js';
const router = express.Router();

router.get('/', (req, res, next) => {
  UserDao.findList().then(data => {
    res.render('user.html', {
      list: data
    })
  }).catch(err => {
    res.render('user.html', {
      list: []
    });
    throw err;
  })
});

router.post('/api/add', (req, res, next) => {
  UserDao.save({
    userName: '李佳玉',
    mobile: 15874940105,
  }).then(data => {
    res.redirect('/user')
  }).catch(err => {
    res.status(500).type('text/plain;charset').end('服务器内部错误');
    throw err;
  })
});

module.exports = {
  path: '/user',
  router
}
